// pages/vipcard/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), common_js = require("../../../component/common.js")
import Toast from '../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.getImgUrl(),
    pageIndex: 1,
    pageSize: 10,
    // products:[],
    signNum: 0,  //签到数 
    min: 1,  //默认值日期第一天1
    max: 7,  //默认值日期最后一天7
    be: 0,  //默认倍数
    scores:[],
    rule:{},
    signRoles:[],
    signDays:0,
    submitState:0,
    today_sign_state:0,
    sign_remark:'',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
  },
  goBack(){
    wx.navigateBack({
      delta: -1,
    })
  },
  onShow: function () {
    var that = this
    var isLogin = app.getStorageSync("isLogin");
    that.setData({ isLogin: isLogin})
    if(isLogin == 0){
      wx.navigateTo({
        url: '/pages/login/index',
      })
    }else{
      this.onHttpInit()
    }
  },
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/farm/signView', {
      pagesize: that.data.pageSize,
      pageindex: that.data.pageIndex,
    }, function (res) {
      console.log("获取签到页面的数据", res)
      if (res.data.code == 0) {
        that.setData({
          signNum: res.data.data.sign_days,
          today_sign_state: res.data.data.today_sign_state,
          scores:res.data.data.scores,
          rule:res.data.data.rule,
          signRoles:res.data.data.signRoles,
          sign_remark: res.data.data.sign_remark,
        })
      }
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
  signClick:function(){
    if(this.data.submitState == 1){
      Toast("不能点太快哟~")
      return;
    }
    this.data.submitState = 1;
    this.setData({
      submitState:1
    })

    var that = this
    fetchApi.httpPost('/farm/sign', {}, function (res) {
      console.log("签到的数据", res)
      if (res.data.code == 0) {
        Toast.success('签到成功');
        that.setData({
          signNum: res.data.data
        })
        that.onHttpInit()
      } else {
        Toast.fail(res.data.msg);
      }
      setTimeout(function(){
        that.data.submitState = 0;
        that.setData({
          submitState:0
        })
      },1000)
      that.setData({ netWork: true, isLoading: false, })
    }, function () {
      that.setData({ netWork: false, hasData: false, isLoading: false, })
    })
  },
})